/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package chapter6;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Denise Carolina
 */
public class Files {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            File file = new File("test.txt");
            System.out.println("file.exists():: " + file.exists());
            boolean exists = file.createNewFile();
            System.out.println("file.exists():: " + exists);
            System.out.println("file.exists():: " + file.exists());

            FileWriter fw = new FileWriter(file);
            fw.write("denise carolina\n");
            fw.write("videtta lopez");
            fw.flush();
            fw.close();

            FileReader fr = new FileReader(file);
            char [] content = new char[40];
            System.out.println("char read:: " + fr.read(content));
            
            for(char c: content){
                    System.out.print(c);
            }
            fr.close();
            System.out.println("");
            // Old way to do it::
            File f = new File("test2.txt");
            FileWriter fw1 = new FileWriter(f);
            BufferedWriter bw1 = new BufferedWriter(fw1);
            bw1.write("test_1"); bw1.newLine(); bw1.write("test_2");
            bw1.flush();
            bw1.close();

            FileReader fr1 = new FileReader(f);
            BufferedReader br1 = new BufferedReader(fr1);
            System.out.println("br1:: " + br1.readLine());
            fr1.close();


        } catch (IOException ex) {
            Logger.getLogger(Files.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}
